﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class tab_productDal
	{
		IDBHelp db = DBHelp.Create();
		 Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="pid">标识列ID</param>
		public bool Exists(int pid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [tab_product] where pid=@pid ");
			htSyn.Add("@pid", pid);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<tab_product> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [createDate],[pid],[pcid],[productInfo],[productKey],[productName],[productTitle],[productDescription] FROM [tab_product]");
			 return gettab_productBySql(strSql.ToString());
		}
		#endregion

		#region 根据标识列查询实体
		/// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="pid">标识列ID</param>
		/// <returns></returns>
		public tab_product GetModel(int pid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [createDate],[pid],[pcid],[productInfo],[productKey],[productName],[productTitle],[productDescription] FROM [tab_product]");
			strSql.Append(" where pid=@pid ");
			htSyn.Add("@pid", pid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			tab_product tab_productModel = new tab_product();
			if (dt.Rows.Count > 0)
			{
				tab_productModel.createDate=Convert.ToDateTime(dt.Rows[0]["createDate"]);
				tab_productModel.pcid=Convert.ToInt32(dt.Rows[0]["pcid"]);
				tab_productModel.productInfo=dt.Rows[0]["productInfo"].ToString();
				tab_productModel.productKey=dt.Rows[0]["productKey"].ToString();
				tab_productModel.productName=dt.Rows[0]["productName"].ToString();
				tab_productModel.productTitle=dt.Rows[0]["productTitle"].ToString();
				tab_productModel.productDescription=dt.Rows[0]["productDescription"].ToString();
			}
			return tab_productModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="tab_product">实体</param>
		/// <returns></returns>
		public bool Update(tab_product tab_productModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [tab_product] set ");
			strSql.Append("[createDate]=@createdate,");
			strSql.Append("[pcid]=@pcid,");
			strSql.Append("[productInfo]=@productinfo,");
			strSql.Append("[productKey]=@productkey,");
			strSql.Append("[productName]=@productname,");
			strSql.Append("[productTitle]=@producttitle,");
			strSql.Append("[productDescription]=@productdescription");

			strSql.Append(" where pid=@pid ");
			htSyn.Add("@createdate", tab_productModel.createDate);
			htSyn.Add("@pid", tab_productModel.pid);
			htSyn.Add("@pcid", tab_productModel.pcid);
			htSyn.Add("@productinfo", tab_productModel.productInfo);
			htSyn.Add("@productkey", tab_productModel.productKey);
			htSyn.Add("@productname", tab_productModel.productName);
			htSyn.Add("@producttitle", tab_productModel.productTitle);
			htSyn.Add("@productdescription", tab_productModel.productDescription);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="pid">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int pid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [tab_product] where pid=@pid");
			htSyn.Add("pid", pid);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="tab_product">实体</param>
		/// <returns></returns>
		public bool Insert(tab_product tab_productModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [tab_product]([createDate],[pcid],[productInfo],[productKey],[productName],[productTitle],[productDescription]) values (@createdate,@pcid,@productinfo,@productkey,@productname,@producttitle,@productdescription)");
			htSyn.Add("@createdate", tab_productModel.createDate);
			htSyn.Add("@pcid", tab_productModel.pcid);
			htSyn.Add("@productinfo", tab_productModel.productInfo);
			htSyn.Add("@productkey", tab_productModel.productKey);
			htSyn.Add("@productname", tab_productModel.productName);
			htSyn.Add("@producttitle", tab_productModel.productTitle);
			htSyn.Add("@productdescription", tab_productModel.productDescription);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<tab_product> gettab_productBySql(string strSql)
		{
			IList<tab_product> list = new List<tab_product>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					tab_product tab_productModel = new tab_product();
					tab_productModel.createDate= Convert.ToDateTime(dr["createDate"]);
					tab_productModel.pid= Convert.ToInt32(dr["pid"]);
					tab_productModel.pcid= Convert.ToInt32(dr["pcid"]);
					tab_productModel.productInfo=dr["productInfo"].ToString();
					tab_productModel.productKey=dr["productKey"].ToString();
					tab_productModel.productName=dr["productName"].ToString();
					tab_productModel.productTitle=dr["productTitle"].ToString();
					tab_productModel.productDescription=dr["productDescription"].ToString();
					list.Add(tab_productModel);
				}
			}
			return list;
		}
		#endregion
	}
}
